﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using AwesomeEnginePC.Physics;

namespace AwesomeEnginePC.Graphics.TwoDimensional.Text
{
    public class BaseString : Base2DObject
    {
        private SpriteFont _fontSprite;
        private string _text;
        private GraphicsDevice _graphicsDevice;

        public GraphicsDevice graphicsDevice
        {
            get { return _graphicsDevice; }
        }

        public SpriteFont FontSprite
        {
            get { return _fontSprite; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public override int Width
        {
            get { return (int)_fontSprite.MeasureString(_text).X; }
            set { } //these will have to figured out later
        }

        public override int Height
        {
            get { return (int)_fontSprite.MeasureString(_text).Y; }
            set { } //these will have to figured out later
        }

        public BaseString(ContentManager CM, string assetName, string text)
            : base(CM)
        {
            _text = text;
            _fontSprite = LoadContent<SpriteFont>(assetName);
           
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector position)
            : base(CM)
        {
            _text = text;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector position, Color tint)
            : base(CM)
        {
            _text = text;
            Position = position;
            Tint = tint;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }

        public BaseString(ContentManager CM, string assetName, string text, Vector position, Color tint, Vector scaling, float rotation)
            : base(CM)
        {
            _text = text;
            Position = position;
            Tint = tint;
            ScalingX = (float)scaling.X;
            ScalingY = (float)ScalingY;
            Rotation = rotation;
            _fontSprite = LoadContent<SpriteFont>(assetName);
        }


        public override void Update(GameTime gameTime)
        {

        }

        public override void Draw(GameTime gameTime, SpriteBatch SB)
        {
            SB.DrawString(_fontSprite, _text, Position.ToVector2(), Tint, Rotation, Origin.ToVector2(), new Vector2(ScalingX, ScalingY), SpriteEffects.None, LayerDepth);
        }

        public void SetGraphicsDevice(GraphicsDevice GD)
        {
            _graphicsDevice = GD;
        }

        
        
        

        /// <summary>
        /// This Function Parses a string to wrap text based on a max word count
        /// </summary>
        /// <param name="maxLineWordCount">Max Amount of words before a New Line</param>
        /// <returns>Returns the New String</returns>
        public String parseWordText(float maxLineWordCount)
        {

            string[] words = Text.Split(' ');
            StringBuilder strbuilder = new StringBuilder();
            int count = 1;
            float spaceWidth = FontSprite.MeasureString(" ").X;
            for (int i = 0; i < words.Length; i++)
            {
                Vector2 size = FontSprite.MeasureString(words[i]);

                if (count < maxLineWordCount)
                {
                    strbuilder.Append(words[i] + " ");
                    count++;   
                }
                else
                {
                    strbuilder.Append("\n" + words[i] + " ");
                    count = 1;    
                }
            }
               
            return strbuilder.ToString();

        }

    }
}
